/*
 * @Description: 自增字段 reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-10-31 17:43:39
 */
import { isNullObj } from '@/utils';

import {
  ADD_SELECT_FIELD_LIST,
  DELETE_SELECT_FIELD_LIST,
  SET_SELECT_AT,
  SET_SELECT_FIELD_LIST,
  SET_SELECT_FOND_ID,
  SET_SELECT_AT_ID
} from '../constants';
import { delItemByIdArray, insertItemAfter } from '@/utils/list';

export const AutoFilingReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的全宗
     */
    case SET_SELECT_FOND_ID:
      return {
        ...state,
        selectFondId: action.id
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT_ID: {
      return {
        ...state,
        selectATId: action.id
      };
    }
    // 设置排序列表
    case SET_SELECT_FIELD_LIST: {
      console.log(action.data === null);
      if (action.data) {
        const _data = action.data;
        const info = {
          commonRule: {
            pageNum: _data.pageNum,
            serialLength: _data.serialLength,
            defaultSpec: _data.defaultSpec,
            id: _data.id
          },
          list: _data.groupFieldResultList
        };

        return {
          ...state,
          ...info,
          loading: false
        };
      } else {
        return {
          ...state,
          list: [],
          commonRule: {}
        };
      }
    }

    // 打开编辑弹窗
    case ADD_SELECT_FIELD_LIST:
      return {
        ...state,
        list: insertItemAfter(action.data, state.list)
      };

    // 打开编辑弹窗
    case DELETE_SELECT_FIELD_LIST: {
      const list = delItemByIdArray(action.ids, state.list);
      return { ...state, list };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
